package org.liu.simpleTank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.TimerTask;
import java.util.Timer;


public final class WindowedMain extends Frame {
	
	private Game game;
	private int offX;
	private int offY;
	{
//		game = new Game(this); buffer image cannot be created 
		// due to incomplete initialization of this component
		// so we move this step further to start method
		offX = 0;
		offY = 0;
	}
	
	public WindowedMain() {
		super("Simple Tank");
		addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				// game may not be initialized immediately after the construction of this component
				if(game != null) 
					game.stop();
			}
			@Override
			public void focusGained(FocusEvent e) {
				if(game != null) 
					game.start();
			}
		});
		addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
				// restart game
				if(e.getKeyChar() == 'r') {
					game.destroy();
					game.init();
					game.start();
				}
			}
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
					Runtime.getRuntime().exit(0);
			}
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		addWindowListener(new WindowListener() {
			@Override
			public void windowOpened(WindowEvent e) { }
			@Override
			public void windowIconified(WindowEvent e) { }
			@Override
			public void windowDeiconified(WindowEvent e) { }
			@Override
			public void windowDeactivated(WindowEvent e) { }
			@Override
			public void windowClosing(WindowEvent e) {
				Runtime.getRuntime().exit(0);
			}
			@Override
			public void windowClosed(WindowEvent e) { }
			@Override
			public void windowActivated(WindowEvent e) { }
		});
	}
	
	public void start() {
		game = new Game(this);
		int newWidth;
		int newHeight;
		Insets is = getInsets();
		newWidth = CONSTS.SW + is.left + is.right;
		newHeight = CONSTS.SH + is.top + is.bottom;
		offX = is.left;
		offY = is.top;
		setSize(newWidth, newHeight);
		game.init();
		game.start();
		Timer t = new Timer(true);
		t.schedule(this.new updateTask(), 0, 16);
	}

	public static void main(String[] args) {
		WindowedMain f = new WindowedMain();

		f.setVisible(true);
		f.start();
	}
	
	@Override
	public void update(Graphics g) {
		paint(g);
	}
	
	@Override
	public void paint(Graphics g) {
		if(game == null)
			return;
		Image img = game.getFrame();
		if(img == null)
			return;
		synchronized (img) {
			g.drawImage(img, offX, offY, null);
		}
	}
	
	class updateTask extends TimerTask {
		@Override
		public void run() {
			Image img = game.getFrame();
			if(img == null)
				return;
			WindowedMain.this.repaint();
			synchronized (img) {
				game.update();
			}
		}
	}
	
}
